package com.wali.modules.system.control;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wali.common.ccp.exception.CcpException;
import com.wali.common.ccp.secret.CcpSecretManager;
import com.wali.common.enums.ErrorCodeEnum;
import com.wali.common.web.BaseController;
import com.wali.modules.system.bo.SysUserBO;
import com.wali.modules.system.enums.UserCodeEnum;
import com.wali.modules.system.service.SysMenuService;
import com.wali.modules.system.service.SysUserService;
import com.wali.modules.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 系统用户相关controller类
 *
 * @author walixu
 * @date 2018年7月13日
 */
@Controller
@RequestMapping(value = {"/system/user"})
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * @param
     * @return
     * @throws
     * @Title updateUserPassword
     * @Description 修改用户密码
     */
    @RequestMapping(value = "/updateUserPassword")
    public ResponseEntity<String> updateUserPassword(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            String oldPassword = request.getParameter("oldPassword");
            final String newPassword = request.getParameter("newPassword");

            HttpSession session = request.getSession(false);
            final SysUserVO user = (SysUserVO) session.getAttribute("user");

            checkOldPassword(user, oldPassword);// 验证旧密码是否正确

            int count = sysUserService.updateUserPassword(new SysUserBO() {{
                this.setId(user.getId());
                this.setPassword(CcpSecretManager.getInstance().encryptMD5(newPassword));
            }});
            if (count != 0) {
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
                request.getSession(false).invalidate();// 销毁session
            }
        } catch (CcpException e) {
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title getSysUser
     * @Description 查询用户基本信息
     */
    @RequestMapping(value = "/getSysUser")
    public ResponseEntity<String> getSysUser(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            HttpSession session = request.getSession(false);
            if (session != null) {
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();

                SysUserVO user = (SysUserVO) session.getAttribute("user");

                SysUserVO result = new SysUserVO();
                result.setUsername(user.getUsername());
                result.setName(user.getName());

                return getResponseEntity(code, msg, result);
            }
        } catch (CcpException e) {
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title listUserMenu
     * @Description 查询用户的菜单列表
     */
    @RequestMapping(value = "/listUserMenu")
    public ResponseEntity<String> listUserMenu(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            HttpSession session = request.getSession(false);
            if (session != null) {
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
                SysUserVO user = (SysUserVO) session.getAttribute("user");
                JSONArray objects = sysMenuService.listSysMenu(user.getRoleId());
                return getResponseEntity(code, msg, objects);
            }
        } catch (CcpException e) {
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title checkOldPassword
     * @Description 验证旧密码是否正确
     */
    private void checkOldPassword(SysUserVO user, String oldPassword) {
        if (!user.getPassword().equals(CcpSecretManager.getInstance().encryptMD5(oldPassword))) {
            throw new CcpException(UserCodeEnum.ERROR_OLD_PASSWORD);
        }
    }

    /**
     * @return
     * @throws
     * @Title getNormalUserMenu
     * @Description 获得普通用户的菜单
     */
    private JSONArray getNormalUserMenu() {
        JSONArray normal_list = new JSONArray();

        JSONObject normal_menu_1 = new JSONObject();
        normal_menu_1.put("icon", "layui-icon-rmb");
        normal_menu_1.put("title", "快递管理");
        normal_menu_1.put("spread", true);

        JSONArray normal_list_1 = new JSONArray();

        JSONObject normal_menu_1_2 = new JSONObject();
        normal_menu_1_2.put("title", "直接下单");
        normal_menu_1_2.put("jump", "/bill/expressAdd");
        normal_list_1.add(normal_menu_1_2);

        JSONObject normal_menu_1_3 = new JSONObject();
        normal_menu_1_3.put("title", "我的运单");
        normal_menu_1_3.put("jump", "/bill/expressList");
        normal_list_1.add(normal_menu_1_3);

        normal_menu_1.put("list", normal_list_1);

        JSONObject normal_menu_2 = new JSONObject();
        normal_menu_2.put("icon", "layui-icon-user");
        normal_menu_2.put("title", "个人设置");

        normal_list.add(normal_menu_1);
        normal_list.add(normal_menu_2);

        return normal_list;
    }

    /**
     * @return
     * @throws
     * @Title getAdminUserMenu
     * @Description 获得管理员用户的菜单
     */
    private JSONArray getAdminUserMenu() {
        JSONArray list = new JSONArray();

        JSONObject menu_1 = new JSONObject();
        menu_1.put("icon", "layui-icon-rmb");
        menu_1.put("title", "快递管理");
        menu_1.put("spread", true);

        JSONArray list_1 = new JSONArray();

        JSONObject menu_1_1 = new JSONObject();
        menu_1_1.put("title", "控制台");
        menu_1_1.put("jump", "/");
        list_1.add(menu_1_1);

        JSONObject menu_1_2 = new JSONObject();
        menu_1_2.put("title", "直接下单");
        menu_1_2.put("jump", "/bil/expressAdd");
        list_1.add(menu_1_2);

        JSONObject menu_1_3 = new JSONObject();
        menu_1_3.put("title", "我的运单");
        menu_1_3.put("jump", "/bil/expressList");
        list_1.add(menu_1_3);

        JSONObject menu_1_4 = new JSONObject();
        menu_1_4.put("title", "商品备案");
        menu_1_4.put("jump", "/com/goodsList");
        list_1.add(menu_1_4);

        menu_1.put("list", list_1);

//        JSONObject menu_2 = new JSONObject();
//        menu_2.put("icon", "layui-icon-user");
//        menu_2.put("title", "个人设置");

        list.add(menu_1);

        return list;
    }
}
